﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using EntityManage;

public partial class Modules_Default_MyAppeal : System.Web.UI.Page
{
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    IOther_AppealHandler appealHandler = new Other_AppealManage();
    IOther_AppealAttachHandler appAttachHandler = new Other_AppealAttachManage();
    IPerf_Flow_TraceHandler flowTraceHandler = new Perf_Flow_TraceManage();
    IPerf_Flow_StepsHandler flowStepHandler = new Perf_Flow_StepsManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "Id";
            ViewState["Accending"] = "asc";

            dataBind(" 1=1 ", new SqlParameter[]{});
          
        }
    }

    /// <summary>
    /// 初始化岗位信息表
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {
            // 读取各上级单位
            int userId = int.Parse(Session["CurrentUserID"].ToString());

            whereClause += " and User_ID = @UserID";
            parms = new SqlParameter[] { new SqlParameter("@UserID", userId) };

            DataTable dt = appealHandler.GetOther_AppealList(whereClause, parms);

            gvAppealList.DataSource = dt;
            gvAppealList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }

    protected void gvAppealList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                // 附件数
                int attNum = appAttachHandler.GetCounter(" Appeal_ID = @Appeal_ID", new SqlParameter[] { new SqlParameter("@Appeal_ID", int.Parse(e.Row.Cells[3].Text)) });
                e.Row.Cells[3].Text = attNum.ToString();

                // 状态
                Enums.OtherAppealStatus status = (Enums.OtherAppealStatus)int.Parse(e.Row.Cells[2].Text);
                e.Row.Cells[2].Text = Enums.GetOtherAppealStatus(status);

                ImageButton btnApproval = (ImageButton)e.Row.FindControl("ibtnApproval");
                if (status == Enums.OtherAppealStatus.NewAdded)
                {
                    btnApproval.Visible = true;
                }
                else
                {
                    btnApproval.Visible = false;
                }

                ImageButton ibtnViewFlow = (ImageButton)e.Row.FindControl("ibtnViewFlow");
                ibtnViewFlow.Visible = status != Enums.OtherAppealStatus.NewAdded;
            }
        }
    }

    protected void gvAppealList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "approval")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString().Trim());
            try
            {
                Other_AppealQuery query = appealHandler.GetOther_AppealQuery(id);
                if (query.Status == (int)Enums.OtherAppealStatus.NewAdded)
                {
                    Perf_Flow_TraceQuery ftQuery = new Perf_Flow_TraceQuery();
                    ftQuery.Flow_ID = (int)Enums.PerfFlowIDs.OtherPersonalPerfAppeal;
                    ftQuery.Business_ID = query.ID;
                    ftQuery.URL = Enums.Other_Personal_Perf_Appeal_Form_URL;
                    ftQuery.Begin_Time = DateTime.Now;
                   //ftQuery.Current_Step = 0;

                    ftQuery.Current_Step = 1;//添加第一步

                    int current_StepID = 0;
                    Perf_Flow_StepsQuery[] querys = flowStepHandler.GetPerf_Flow_StepsQueryList("Flow_ID=@Flow_ID and IsEnable=@IsEnable", new SqlParameter[] { new SqlParameter("@Flow_ID", (int)Enums.PerfFlowIDs.OtherPersonalPerfAppeal), new SqlParameter("@IsEnable", 1) }, " Step_Num asc");
                    if (querys != null && querys.Length > 0)
                    {
                        current_StepID = querys[0].ID;
                    }
                    ftQuery.Current_Step_ID = current_StepID;


                    int flowTraceId = 0;
                    flowTraceHandler.InsertPerf_Flow_Trace(ftQuery, ref flowTraceId);

                    // 更新状态和流程ID
                    if (flowTraceId != 0)
                    {
                        query.Status = (int)Enums.OtherAppealStatus.Approval;
                        query.Flow_Trace_ID = flowTraceId;
                        appealHandler.UpdateOther_Appeal(query);

                        Jscript.AlertInAjax("操作成功", UpdatePanel1);
                    }
                    else
                    {
                        Jscript.AlertInAjax("操作失败", UpdatePanel1);
                    }

                    dataBind(" 1=1 ", new SqlParameter[] { });
                }
            }
            catch
            {
                Jscript.AlertInAjax("操作失败!", UpdatePanel1);
            }
        }
    }

}
